import clojure.lang.Obj;
import entry.Cat;
import entry.Persion;
import kafka.security.auth.All;
import org.mvel2.MVEL;
import org.mvel2.ParserContext;
import org.mvel2.ast.ASTNode;
import org.mvel2.integration.Interceptor;
import org.mvel2.integration.VariableResolverFactory;
import org.mvel2.templates.CompiledTemplate;
import org.mvel2.templates.TemplateCompiler;
import org.mvel2.templates.TemplateRuntime;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.math.MathContext;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by mwang on 2017/1/17.
 */

public class MvelMain {
    public static void main(String args[]) throws Exception {
        Map<String,Object> map = new HashMap<String, Object>();
        Persion p = new Persion();
        p.setName("mwang");
        p.setAge("22");
        //cat
        Cat cat1 = new Cat("mimi", "black");
        Cat cat2 = new Cat("xixi", "yellow");
        Cat cat3 = new Cat("lulu", "white");
        List<Cat> catList = new ArrayList<Cat>();
        catList.add(cat1);
        catList.add(cat2);
        catList.add(cat3);
        p.setCatList(catList);

        map.put("persion",p);


        ParserContext context = new ParserContext();
        Map<String,Interceptor> interceptorMap = new HashMap<String,Interceptor>();
        interceptorMap.put("myInterceptor", new Interceptor() {
            public int doBefore(ASTNode node, VariableResolverFactory factory) {
                System.out.println("BEFORE!");
                return NORMAL_FLOW;
            }

            public int doAfter(Object exitStackValue, ASTNode node, VariableResolverFactory factory) {
                System.out.println("After!");
                return NORMAL_FLOW;
            }
        });
        context.setInterceptors(interceptorMap);

        CompiledTemplate ct = TemplateCompiler.compileTemplate(new File("/Users/mwang/IdeaProjects/mvel_test/src/main/resources/template/template.xml"));
        String resule = TemplateRuntime.execute(ct,context,map).toString();
        System.out.println(resule);

        MVEL.executeExpression(MVEL.compileExpression("System.err.println('MIDDLE');",
                null,
                interceptorMap));


        int READ = 1 << 0;  //1
        int WRITE = 1 << 1; //2
        int CREATE = 1 << 2; //4
        int DELETE = 1 << 3;  //8
        int ADMIN = 1 << 4;
        int ALL = READ | WRITE ;

        System.out.println(READ);
        System.out.println(WRITE);
        System.out.println(CREATE);
        System.out.println(DELETE);
        System.out.println(ALL);
    }
}
